import {
  defineConfig,
  loadEnv
} from 'vite'
import vue from '@vitejs/plugin-vue'
import {
  resolve
} from 'path'
import legacy from '@vitejs/plugin-legacy'
import Components from 'unplugin-vue-components/vite'
import AutoImport from 'unplugin-auto-import/vite'
import vueSetupExtend from 'vite-plugin-vue-setup-extend'
import {
  VantResolver
} from '@vant/auto-import-resolver'

export default defineConfig(({
  mode
}) => {
  const env = loadEnv(mode, process.cwd())
  // 打包名称
  let fileName = 'dist'
  fileName = env.VITE_APP_NAME
  return {
    // host: ''192.168.0.252 这里改为自己的 IP 地址可以让同事直接访问自己本地，方便及时看到效果
    // base: '192.068.0.252',
    base: './', // 这里更改打包相对绝对路径
    plugins: [
      vue(),
      vueSetupExtend(),
      AutoImport({
        imports: ['vue', 'vue-router'],
      }),
      legacy({
        targets: ['defaults', 'not IE 11']
      }),
      Components({
        resolvers: [VantResolver()]
      })
    ],
    server: {
      port: Number(env.VITE_PORT),
      host: '0.0.0.0',
      https: false,
      cors: true,
      open: env.VITE_OPEN == 'true' ? true : false,
      proxy: {
        [env.VITE_BASE_API]: {
          target: env.VITE_API_BASE_URL,
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, '')
        }
      }
    },
    resolve: {
      alias: {
        '@/': `${resolve(__dirname, 'src')}/`
      }
    },
    define: {
      'process.env': {}
    },
    build: {
      target: 'modules',
      outDir: fileName,
      assetsDir: 'static',
      minify: 'terser', // 混淆器，terser构建后文件体积更小
    },
    css: {
      preprocessorOptions: {
        scss: {
          // 关闭编译时 字符编码 报错问题
          charset: true,
          additionalData: `@import "@/assets/styles/variables.scss";`
        }
      }
    }
  }
})
